package com.example.lab1;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {
    public LinkedList<Problem> linkedList = new LinkedList<>();
    public int index;

    public LinkedList<Problem> getLinkedList() {
        return linkedList;
    }

    public void setLinkedList(LinkedList<Problem> linkedList) {
        this.linkedList = linkedList;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int score;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinkedList<Problem> problems = new LinkedList<>();
        problems.add(new Problem(1, "地球是圆的吗?", "是", "是", "不是", "可能是", "一定不是"));
        problems.add(new Problem(2, "太阳属于什么系?", "银河系", "地月系", "太阳系", "都不是", "银河系"));
        problems.add(new Problem(3, "太阳是圆的吗?", "是", "是", "不是", "可能是", "一定不是"));
        problems.add(new Problem(4, "月亮是自己发光的吗?", "不是", "是", "不是", "可能是", "一定不是"));
        problems.add(new Problem(5, "冬天一定很冷吗?", "是", "是", "不是", "可能是", "一定不是"));

        setLinkedList(problems);
        setIndex(0);
        setScore(0);
        //final int index;
        TextView textInfo = (TextView) findViewById(R.id.infor);
        TextView problem = findViewById(R.id.text_view);
        RadioButton radioButtonA = (RadioButton) findViewById(R.id.answerA);
        RadioButton radioButtonB = (RadioButton) findViewById(R.id.answerB);
        RadioButton radioButtonC = (RadioButton) findViewById(R.id.answerC);
        RadioButton radioButtonD = (RadioButton) findViewById(R.id.answerD);


        problem.setText(linkedList.get(0).getProblem());
        radioButtonA.setText(linkedList.get(0).getAnswerA());
        radioButtonB.setText(linkedList.get(0).getAnswerB());
        radioButtonC.setText(linkedList.get(0).getAnswerC());
        radioButtonD.setText(linkedList.get(0).getAnswerD());

        //下一题
        Button button_next = (Button) findViewById(R.id.button_next);
        button_next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                textInfo.setText("");

                if (linkedList.get(index).getAnswer() == radioButtonA.getText() && radioButtonA.isChecked()) {
                    score += 20;
                } else if (linkedList.get(index).getAnswer() == radioButtonB.getText() && radioButtonB.isChecked()) {
                    score += 20;
                } else if (linkedList.get(index).getAnswer() == radioButtonC.getText() && radioButtonC.isChecked()) {
                    score += 20;
                } else if (linkedList.get(index).getAnswer() == radioButtonD.getText() && radioButtonD.isChecked()) {
                    score += 20;
                }
                if (index + 1 < linkedList.size()) {
                    radioButtonA.setChecked(false);
                    radioButtonB.setChecked(false);
                    radioButtonC.setChecked(false);
                    radioButtonD.setChecked(false);
                    problem.setText(linkedList.get(index + 1).getProblem());
                    radioButtonA.setText(linkedList.get(index + 1).getAnswerA());
                    radioButtonB.setText(linkedList.get(index + 1).getAnswerB());
                    radioButtonC.setText(linkedList.get(index + 1).getAnswerC());
                    radioButtonD.setText(linkedList.get(index + 1).getAnswerD());
                    index++;

                } else {
                    Toast.makeText(MainActivity.this, "已经是最后一题了", Toast.LENGTH_SHORT).show();
                }

            }
        });

        Button button_front = (Button) findViewById(R.id.button_front);
        button_front.setOnClickListener(new View.OnClickListener() {//上一题
            @Override
            public void onClick(View v) {
                textInfo.setText("");
                if (linkedList.get(index).getAnswer() == radioButtonA.getText() && radioButtonA.isChecked()) {
                    score += 20;
                } else if (linkedList.get(index).getAnswer() == radioButtonB.getText() && radioButtonB.isChecked()) {
                    score += 20;
                } else if (linkedList.get(index).getAnswer() == radioButtonC.getText() && radioButtonC.isChecked()) {
                    score += 20;
                } else if (linkedList.get(index).getAnswer() == radioButtonD.getText() && radioButtonD.isChecked()) {
                    score += 20;
                }

                if (index - 1 >= 0) {
                    radioButtonA.setChecked(false);
                    radioButtonB.setChecked(false);
                    radioButtonC.setChecked(false);
                    radioButtonD.setChecked(false);
                    problem.setText(linkedList.get(index - 1).getProblem());
                    radioButtonA.setText(linkedList.get(index - 1).getAnswerA());
                    radioButtonB.setText(linkedList.get(index - 1).getAnswerB());
                    radioButtonC.setText(linkedList.get(index - 1).getAnswerC());
                    radioButtonD.setText(linkedList.get(index - 1).getAnswerD());
                    index--;

                } else {
                    Toast.makeText(MainActivity.this, " 没有上一题了", Toast.LENGTH_SHORT).show();
                }
            }
        });

        Button button_submit = (Button) findViewById(R.id.button_submit);

        button_submit.setOnClickListener(new View.OnClickListener() {//查看成绩按钮
            @Override
            public void onClick(View v) {
                textInfo.setText("您的成绩为: " + (score > 100 ? 100 : score));
            }
        });

        //查看答案
        Button button_showAnswer = (Button) findViewById(R.id.button_answer);
        button_showAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textInfo.setText("当前答案为: " + linkedList.get(index).getAnswer());
            }
        });
    }
}